package com.hgkj.cloud.base.service.impl;

import com.hgkj.cloud.base.common.PageInfoParams;
import com.hgkj.cloud.base.dao.EUserMapper;
import com.hgkj.cloud.base.domain.EUser;
import com.hgkj.cloud.base.common.PageInfo;
import com.hgkj.cloud.base.service.EUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 缓存机制说明：所有有缓存注解的查询结果都放进了缓存，也就是把MySQL查询的结果放到了redis中去，
 * 然后第二次发起该条查询时就可以从redis中去读取查询的结果，从而不与MySQL交互，从而达到优化的效果，
 * redis的查询速度之于MySQL的查询速度相当于 内存读写速度 /硬盘读写速度
 *
 * @Cacheable(value="xxx" key="zzz")注解：标注该方法查询的结果进入缓存，再次访问时直接读取缓存中的数据
 * 1.对于有参数的方法，指定value(缓存区间)和key(缓存的key)；
 * 	   对于无参数的方法，只需指定value,存到数据库中数据的key通过com.ssm.utils.RedisCacheConfig中重写的generate()方法生成。
 * 2.调用该注解标识的方法时，会根据value和key去redis缓存中查找数据，如果查找不到，则去数据库中查找，然后将查找到的数据存放入redis缓存中；
 * 3.向redis中填充的数据分为两部分：
 * 		1).用来记录xxx缓存区间中的缓存数据的key的xxx~keys(zset类型)
 * 		2).缓存的数据，key：数据的key；value：序列化后的从数据库中得到的数据
 * 4.第一次执行@Cacheable注解标识的方法，会在redis中新增上面两条数据
 * 5.非第一次执行@Cacheable注解标识的方法，若未从redis中查找到数据，则执行从数据库中查找，并：
 * 		1).新增从数据库中查找到的数据
 * 		2).在对应的zset类型的用来记录缓存区间中键的数据中新增一个值，新增的value为上一步新增的数据的key
 */

@Slf4j
@Service
public class EUserServiceImpl implements EUserService {

	@Autowired
	private EUserMapper eUserMapper;

	@Override
	public int insertUser(EUser user) {
		return eUserMapper.insertUser(user);
	}

	@Override
	public int updateUser(EUser user) {
		return eUserMapper.updateUser(user);
	}

	@Override
	public int resetUserPwd(EUser user) {
		return eUserMapper.resetUserPwd(user);
	}

	public int delUserById(String uuid) {
		return eUserMapper.delUserByUuid(uuid);
	}

	public int PhysicsDelUserById(String uuid) {
		return eUserMapper.PhysicsDelUserByUuid(uuid);
	}


	@Override
	public EUser getUserInfo(Map<String, Object> map) {
		return eUserMapper.getUserInfo(map);
	}

	@Override
	public EUser getBindUserInfo(Map<String, Object> map) {
		return eUserMapper.getBindUserInfo(map);
	}

	@Override
	public Map<String, Object> getUserInfoSimple(String uuid) {
		return eUserMapper.getUserInfoSimple(uuid);
	}

	@Override
	public EUser getUserInfoWithLastUse(Map<String, Object> map){
		return eUserMapper.getUserInfoWithLastUse(map);
	}

	@Override
	public List<EUser> listEUsers(PageInfo pi, Map<String, Object> map) {
		return eUserMapper.listEUsers(PageInfoParams.buildMap(pi, map));
	}

	@Override
	public List<EUser> listEUsersWithAppUsed() {
		return eUserMapper.listEUsersWithAppUsed();
	}

	@Override
	public long listEUsersCount(Map<String, Object> map) {
		return eUserMapper.listEUsersCount(map);
	}

	@Override
	public List<Map<String, Object>> listChannelUsersCount(Map<String, Object> map) {
		return eUserMapper.listChannelUsersCount(map);
	}

	@Override
	public List<EUser> listPrivateEUsers(Map<String, Object> map) {
		return eUserMapper.listPrivateEUsers(map);
	}

	@Override
	public List<EUser> listBraceletDataPrivateEUsers(Map<String, Object> map){
		return eUserMapper.listBraceletDataPrivateEUsers(map);
	}

	@Override
	public int listBraceletDataPrivateEUsersCount(Map<String, Object> map) {
		return eUserMapper.listBraceletDataPrivateEUsersCount(map);
	}

	@Override
	public List<EUser> listBraceletZxbPrivateEUsers(Map<String, Object> map){
		return eUserMapper.listBraceletZxbPrivateEUsers(map);
	}

	@Override
	public int listBraceletZxbPrivateEUsersCount(Map<String, Object> map) {
		return eUserMapper.listBraceletZxbPrivateEUsersCount(map);
	}

	@Override
	public List<EUser> listMattressPrivateEUsers(Map<String, Object> map){
		return eUserMapper.listMattressPrivateEUsers(map);
	}

	@Override
	public int listMattressPrivateEUsersCount(Map<String, Object> map) {
		return eUserMapper.listMattressPrivateEUsersCount(map);
	}

	@Override
	public List<EUser> listTenantEUsers(PageInfo pi, Map<String, Object> map) {
		return eUserMapper.listTenantEUsers(PageInfoParams.buildMap(pi, map));
	}

	@Override
	public List<Map<String, Object>> listReportViewer(Map<String, Object> map) {
		return eUserMapper.listReportViewer(map);
	}

	@Override
	public List<EUser> listExamineUsersByGender(Map<String, Object> map) {
		return eUserMapper.listExamineUsersByGender(map);
	}

	@Override
	public Map<String, Object> getAbnormalRiskUser(Map<String, Object> map) {
		return eUserMapper.getAbnormalRiskUser(map);
	}

	@Override
	public List<Map<String, Object>> listAllUser(Map<String, Object> map) {
		return eUserMapper.listAllUser(map);
	}

	@Override
	public List<EUser> listExamineUsers(PageInfo pi, Map<String, Object> map) {
		return eUserMapper.listExamineUsers(PageInfoParams.buildMap(pi, map));
	}

	@Override
	public int listExamineUsersCount(Map<String, Object> map) {
		return eUserMapper.listExamineUsersCount(map);
	}

	@Override
	public EUser getExamineUserInfo(Map<String, Object> map) {
		return eUserMapper.getExamineUserInfo(map);
	}

	@Override
	public int delUserByUuids(List<String> uuids) {
		return eUserMapper.delUserByUuids(uuids);
	}

	@Override
	public List<EUser> listUsers(PageInfo pi, Map<String, Object> map) {
		return eUserMapper.listUsers(PageInfoParams.buildMap(pi, map));
	}

	@Override
	public long listUsersCount(Map<String, Object> map) {
		return eUserMapper.listUsersCount(map);
	}

	@Override
	public List<EUser> listTableTaskUsers(PageInfo pi, Map<String, Object> map) {
		return eUserMapper.listTableTaskUsers(PageInfoParams.buildMap(pi, map));
	}

	@Override
	public long listTableTaskUsersCount(Map<String, Object> map) {
		return eUserMapper.listTableTaskUsersCount(map);
	}

	@Override
	public int updateUserByFrameworkId(Long frameworkId) {
		return 0;
	}

	@Override
	public List<String> listUserUuid(Map<String, Object> map) {
		return eUserMapper.listUserUuid(map);
	}

	@Override
	public List<EUser> getUserInfoListForExcel(Map<String, Object> map) {
		List<EUser> eUserList = new ArrayList<>();
		Object userExcelList = map.get("UserExcelList");
		Object channel_id = map.get("channel_id");
		// if ()
		List<String> alist = new ArrayList<>();
		if (userExcelList instanceof ArrayList<?>) {
			for (Object o : (List<?>) userExcelList) {
				alist.add(String.class.cast(o));
			}
		}
		EUser userInfo = null;
		Map<String, Object> mapTemp = new HashMap<>();
		mapTemp.put("channel_id", channel_id);
		for (String anAlist : alist) {
			mapTemp.put("user_name", anAlist);
			userInfo = eUserMapper.getUserInfoSimpleByName(mapTemp);
			if (userInfo != null) eUserList.add(userInfo);
		}
		return eUserList;
	}
	@Override
	public List<EUser> listQnTaskUsers(Map<String, Object> map) {
		return eUserMapper.listQnTaskUsers(map);
	}
	@Override
	public long listQnTaskUsersCount(Map<String, Object> map) {
		return eUserMapper.listQnTaskUsersCount(map);
	}

	@Override
	public List<String> getUserIDsByNameList(Map<String, Object> map) {
		Object userExcelList = map.get("UserNameList");
		Object channel_id = map.get("channel_id");
		List<String> alist = new ArrayList<>();
		if (userExcelList instanceof ArrayList<?>) {
			for (Object o : (List<?>) userExcelList) {
				alist.add(String.class.cast(o));
			}
		}
		Map<String, Object> mapTemp = new HashMap<>();
		mapTemp.put("channel_id", channel_id);
		mapTemp.put("user_name", alist);
		List<String> stringList = eUserMapper.listUserUuid(mapTemp);
		return stringList;
	}
}
